
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Event 事件 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="laiq">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-terminal/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Request.html" />
    
    
    <link rel="prev" href="HTTP作用域、上下文.html" />
    

    
        <link rel="shortcut icon" href='../../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="bookmark" href='../../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../../assets/images/wukong.png'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../../assets/images/wukong.png">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../../assets/images/wukong.png">
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="HTTP作用域、上下文.html">
            
                <a href="HTTP作用域、上下文.html">
            
                    
                    Node 学习
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="HTTP作用域、上下文.html">
            
                <a href="HTTP作用域、上下文.html">
            
                    
                    HTTP 作用域、上下文
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.2" data-path="Event事件.html">
            
                <a href="Event事件.html">
            
                    
                    Event 事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="Request.html">
            
                <a href="Request.html">
            
                    
                    Request
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="爬虫.html">
            
                <a href="爬虫.html">
            
                    
                    爬虫
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="HTTP跨域.html">
            
                <a href="HTTP跨域.html">
            
                    
                    HTTP 跨域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="HTML5新特性.html">
            
                <a href="HTML5新特性.html">
            
                    
                    HTML5 新特性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../php/设计模式/">
            
                <a href="../php/设计模式/">
            
                    
                    PHP 学习
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../php/设计模式/">
            
                <a href="../php/设计模式/">
            
                    
                    设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1.1" data-path="../php/设计模式/1.单例模式.html">
            
                <a href="../php/设计模式/1.单例模式.html">
            
                    
                    1.单例模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.2" data-path="../php/设计模式/2.抽象工厂模式.html">
            
                <a href="../php/设计模式/2.抽象工厂模式.html">
            
                    
                    2.抽象工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.3" data-path="../php/设计模式/3.构造模式.html">
            
                <a href="../php/设计模式/3.构造模式.html">
            
                    
                    3.构造模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.4" data-path="../php/设计模式/4.原型模式.html">
            
                <a href="../php/设计模式/4.原型模式.html">
            
                    
                    4.原型模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.5" data-path="../php/设计模式/5.工厂方法模式.md">
            
                <span>
            
                    
                    5.工厂方法模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../php/随手笔记/">
            
                <a href="../php/随手笔记/">
            
                    
                    随手笔记
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../php/随手笔记/PHP页面静态化.html">
            
                <a href="../php/随手笔记/PHP页面静态化.html">
            
                    
                    PHP页面静态化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../interview/">
            
                <a href="../interview/">
            
                    
                    面试相关
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Event 事件</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="nodejs-eventemitter">Node.js EventEmitter</h1>
<p>Node.js &#x6240;&#x6709;&#x7684;&#x5F02;&#x6B65; I/O &#x64CD;&#x4F5C;&#x5728;&#x5B8C;&#x6210;&#x65F6;&#x90FD;&#x4F1A;&#x53D1;&#x9001;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x5230;&#x4E8B;&#x4EF6;&#x961F;&#x5217;&#x3002;</p>
<p>Node.js &#x91CC;&#x9762;&#x7684;&#x8BB8;&#x591A;&#x5BF9;&#x8C61;&#x90FD;&#x4F1A;&#x5206;&#x53D1;&#x4E8B;&#x4EF6;&#xFF1A;&#x4E00;&#x4E2A; <code>net.Server</code> &#x5BF9;&#x8C61;&#x4F1A;&#x5728;&#x6BCF;&#x6B21;&#x6709;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#xFF0C; &#x4E00;&#x4E2A; <code>fs.readStream</code> &#x5BF9;&#x8C61;&#x4F1A;&#x5728;&#x6587;&#x4EF6;&#x88AB;&#x6253;&#x5F00;&#x7684;&#x65F6;&#x5019;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x3002; &#x6240;&#x6709;&#x8FD9;&#x4E9B;&#x4EA7;&#x751F;&#x4E8B;&#x4EF6;&#x7684;&#x5BF9;&#x8C61;&#x90FD;&#x662F; <code>events.EventEmitter</code> &#x7684;&#x5B9E;&#x4F8B;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> EventEmitter = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;events&apos;</span>).EventEmitter
<span class="hljs-keyword">var</span> life = <span class="hljs-keyword">new</span> EventEmitter()

life.on(<span class="hljs-string">&apos;e1&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">who</span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7ED9; &apos;</span> + who + <span class="hljs-string">&apos;&#x5012;&#x6C34;&apos;</span>)
})
life.on(<span class="hljs-string">&apos;e1&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">who</span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7ED9; &apos;</span> + who + <span class="hljs-string">&apos;&#x6D17;&#x8863;&#x670D;&apos;</span>)
})
life.on(<span class="hljs-string">&apos;e1&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">who</span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7ED9; &apos;</span> + who + <span class="hljs-string">&apos;&#x62D6;&#x5730;&apos;</span>)
})
life.on(<span class="hljs-string">&apos;e1&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">who</span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7ED9; &apos;</span> + who + <span class="hljs-string">&apos;&#x505A;&#x996D;&apos;</span>)
})

<span class="hljs-comment">// &#x89E6;&#x53D1;&#x4E8B;&#x4EF6;</span>
life.emit(<span class="hljs-string">&apos;e1&apos;</span>, <span class="hljs-string">&apos;kuli&apos;</span>)
</code></pre>
<pre><code>**[terminal]
**[path ~]**[delimiter  $ ]**[command node event.js]
&#x7ED9; kuli&#x5012;&#x6C34;
&#x7ED9; kuli&#x6D17;&#x8863;&#x670D;
&#x7ED9; kuli&#x62D6;&#x5730;
&#x7ED9; kuli&#x505A;&#x996D;
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="HTTP作用域、上下文.html" class="navigation navigation-prev " aria-label="Previous page: HTTP 作用域、上下文">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Request.html" class="navigation navigation-next " aria-label="Next page: Request">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Event 事件","level":"1.2.2","depth":2,"next":{"title":"Request","level":"1.2.3","depth":2,"path":"docs/node/Request.md","ref":"docs/node/Request.md","articles":[]},"previous":{"title":"HTTP 作用域、上下文","level":"1.2.1","depth":2,"path":"docs/node/HTTP作用域、上下文.md","ref":"docs/node/HTTP作用域、上下文.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","include-codeblock","alerts","sitemap-general","favicon","github","terminal","toggle-chapters"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://gitee.com/tonygeli/"},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sitemap-general":{"prefix":"http://gitbook.zhangjikai.com"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":{"shortcut":"./assets/images/favicon.ico","bookmark":"./assets/images/favicon.ico","appleTouch":"./assets/images/wukong.png","appleTouchMore":{"120x120":"./assets/images/wukong.png","180x180":"./assets/images/wukong.png"}},"alerts":{},"include-codeblock":{"check":false,"edit":false,"fixlang":false,"lang":"","template":"ace","theme":"monokai","unindent":true},"terminal":{"copyButtons":true,"fade":false,"style":"flat"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{}},"theme":"default","author":"laiq","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*","description":"学习记录与分享"},"file":{"path":"docs/node/Event事件.md","mtime":"2018-10-19T02:29:20.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-11-01T05:28:55.014Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-terminal/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

